package com.hust.domain;

import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Data
public class ApportionResult {

    private List<Double> components;

    private List<Double> comp1_PH;

    private List<Double> comp1_DO;

    private List<Double> comp1_IMn;

    private List<Double> comp1_COD;

    private List<Double> comp1_BOD5;

    private List<Double> comp1_NH3N;

    private List<Double> comp1_TP;

    private List<Double> comp2_PH;

    private List<Double> comp2_DO;

    private List<Double> comp2_IMn;

    private List<Double> comp2_COD;

    private List<Double> comp2_BOD5;

    private List<Double> comp2_NH3N;

    private List<Double> comp2_TP;

    private List<Double> comp3_PH;

    private List<Double> comp3_DO;

    private List<Double> comp3_IMn;

    private List<Double> comp3_COD;

    private List<Double> comp3_BOD5;

    private List<Double> comp3_NH3N;

    private List<Double> comp3_TP;

    private List<Double> comp4_PH;

    private List<Double> comp4_DO;

    private List<Double> comp4_IMn;

    private List<Double> comp4_COD;

    private List<Double> comp4_BOD5;

    private List<Double> comp4_NH3N;

    private List<Double> comp4_TP;

    private List<Double> comp5_PH;

    private List<Double> comp5_DO;

    private List<Double> comp5_IMn;

    private List<Double> comp5_COD;

    private List<Double> comp5_BOD5;

    private List<Double> comp5_NH3N;

    private List<Double> comp5_TP;

    private List<Double> comp6_PH;

    private List<Double> comp6_DO;

    private List<Double> comp6_IMn;

    private List<Double> comp6_COD;

    private List<Double> comp6_BOD5;

    private List<Double> comp6_NH3N;

    private List<Double> comp6_TP;

    private List<Double> comp7_PH;

    private List<Double> comp7_DO;

    private List<Double> comp7_IMn;

    private List<Double> comp7_COD;

    private List<Double> comp7_BOD5;

    private List<Double> comp7_NH3N;

    private List<Double> comp7_TP;

    private List<Double> comp8_PH;

    private List<Double> comp8_DO;

    private List<Double> comp8_IMn;

    private List<Double> comp8_COD;

    private List<Double> comp8_BOD5;

    private List<Double> comp8_NH3N;

    private List<Double> comp8_TP;

    public void FromXlsx(String filename) {

        try {
            //获取文件流
            FileInputStream inputStream = new FileInputStream(filename);
            //1.创建工作簿,使用excel能操作的这边都看看操作
            Workbook workbook = new XSSFWorkbook(inputStream);
            //读取工作簿1：主成分信息
            Sheet sheetComp = workbook.getSheetAt(0);
            Row rowComp = sheetComp.getRow(1);
            components = new ArrayList<>();
            for (int i = 0; i < rowComp.getLastCellNum(); i++) {
                Cell cell = rowComp.getCell(i);
                components.add(cell.getCellType() == CellType.NUMERIC ?
                        cell.getNumericCellValue() : Double.valueOf(cell.getStringCellValue()));
            }

            String[] names = {"PH", "DO", "IMn", "COD", "BOD5", "NH3N", "TP"};
            for (int i = 1; i < workbook.getNumberOfSheets(); i++) {
                //2.得到表
                Sheet sheet = workbook.getSheetAt(i);
                int headRow = 1;
                int headCol = 1;
                for (int j = headRow; j <= sheet.getLastRowNum(); j++) {
                    //3.得到行
                    Row row = sheet.getRow(j);
                    List<Double> data = new ArrayList<>();
                    for (int k = headCol; k < row.getLastCellNum(); k++) {
                        //4.得到列
                        Cell cell = row.getCell(k);
                        data.add(cell.getCellType() == CellType.NUMERIC ?
                                cell.getNumericCellValue() : Double.valueOf(cell.getStringCellValue()));
                    }
                    String fieldName = String.format("comp%d_%s", i, names[j - headRow]);
                    Field field = this.getClass().getDeclaredField(fieldName);
                    field.setAccessible(true); // 使私有字段可访问
                    field.set(this, data);
                }
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
